//
//  ZYLTabBar.m
//  XXWeiBo
//
//  Created by mac on 15-1-31.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "ZYLTabBar.h"
#import "ZYLTabBarButton.h"

@interface ZYLTabBar ()

@property (nonatomic, weak) ZYLTabBarButton *currenSelectedBtn;

@property (nonatomic, weak) UIButton *plusBtn;

@property (nonatomic, strong) NSMutableArray *btns;

@end
@implementation ZYLTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
- (void)setup
{
    self.backgroundColor = [UIColor whiteColor];
    
    [self addPlusBtn];
}

- (void)addPlusBtn
{
    UIButton *plusBtn = [[UIButton alloc] init];
    plusBtn.tag = 998;
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 3.设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [self addSubview:plusBtn];
    
    self.plusBtn = plusBtn;
}

- (void)addItem:(UITabBarItem *)item
{
    ZYLTabBarButton *btn = [[ZYLTabBarButton alloc] init];
    
    btn.item = item;
    
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:btn];
    
    btn.tag = self.btns.count;
    [self.btns addObject:btn];
    
    if (self.btns.count == 1) {
        [self btnOnClick:btn];
    }
}
- (void)btnOnClick:(ZYLTabBarButton *)btn
{
    self.currenSelectedBtn.selected = NO;
    
    btn.selected = YES;
    
    self.currenSelectedBtn = btn;
    
    [UIView animateWithDuration:0.2 animations:^{
        btn.transform = CGAffineTransformMakeScale(0.4, 0.4);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            btn.transform = CGAffineTransformMakeScale(1.4, 1.4);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1 animations:^{
                btn.transform = CGAffineTransformIdentity;
            }];
    
        }];
    }];
    if ([self.delegate respondsToSelector:@selector(tabBar:selectedBtnFrom:to:)]) {
        [self.delegate tabBar:self selectedBtnFrom:self.currenSelectedBtn.tag to:btn.tag];
    }

}

- (void)layoutSubviews
{
    NSUInteger index = 0;
    for (UIView *child in self.subviews) {
        if (child.tag != 998) {
            CGFloat childW = self.frame.size.width / 5;
            CGFloat childH = self.frame.size.height;
            CGFloat childX = index * childW;
            CGFloat childY = 0;
            
            if (index == 2) {
                index++;
                childX = index * childW;
            }
            index++;
            child.frame = CGRectMake(childX, childY, childW, childH);
        }
    }
    
    self.plusBtn.size = self.plusBtn.currentBackgroundImage.size;
    self.plusBtn.centerX = self.wideth * 0.5;
    self.plusBtn.centerY = self.height * 0.5;
    
}

- (NSMutableArray *)btns
{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}
@end
